let sub = document.querySelector('.sub')
let add = document.querySelector('.add')
let pages = document.querySelector('[colspan="4"] input')
let bookAdd = document.querySelector('h1 input[value*="新增"]')
let bookKey = document.querySelector('h1 input[value*="搜索"]')
let keyword = document.querySelector('h1 [type*="text"]')

//前一页
sub.addEventListener('click',() => {
    pages.value = +pages.value === 1 ? 1 : +pages.value - 1
    location.href = `/bookList?pages=${pages.value}&keyword=${keyword.value}`
})

//后一页
add.addEventListener('click',e => {
    pages.value = +pages.value == +add.dataset.id ? +add.dataset.id : +pages.value + 1
    location.href = `/bookList?pages=${pages.value}&keyword=${keyword.value}`
})

//输入页数
pages.addEventListener('blur',() => {
    if(!(+pages.value)){
        pages.value = 1
    }
    if(+pages.value <= 0){
        pages.value = 1
    }
    if(+pages.value >= +add.dataset.id){
        pages.value = +add.dataset.id
    }
    location.href = `/bookList?pages=${pages.value}&keyword=${keyword.value}`
})

//新增跳转
bookAdd.addEventListener('click',() => location.href = '/bookAdd')


document.querySelector('table').addEventListener('click',(e) => {
    //删除跳转
    if (e.target.className === 'update') {
        location.href = `/bookUpdate/${e.target.dataset.id}`
    }
    //编辑跳转
    if (e.target.className === 'delete') {
        location.href = `/bookDelete/${e.target.dataset.id}`
    }
})

bookKey.addEventListener('click',() => {
    location.href = `/bookList?pages=1&keyword=${keyword.value}`
})


